package com.example.myapplication1.dinybehaivor.behavior;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EdgeEffect;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import androidx.core.widget.NestedScrollView;

import com.example.myapplication1.R;
import com.example.myapplication1.loldemo.LolRecordQueryActivity;

import okhttp3.OkHttpClient;


/**
 * Copyright (C), 2020, 重庆八进制有限公司
 *
 * @author: weng
 * @last-modifier: weng
 * @version: 1.0
 * @create-date: 2020/12/22 15:10
 * @last-modify-date: 2020/12/22 15:10
 * @description:
 */
public class ViewScaleBehavior extends CoordinatorLayout.Behavior {

    private static final String TAG = "ViewScaleBehavior";
    private Context mContext;
    private int mMaxScale = 4;
    private int mHeaderHeight;
    private int mHeaderWidth;
    private float mSlidingOffset = 0.5F;//滑动阻力系数
    private boolean isinit;

    public ViewScaleBehavior() {
    }

    public ViewScaleBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }


    @Override
    public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
        Log.e("tag","layoutDependsOn");
       if (!isinit){

           init(child);
           isinit = true;
       }

        return dependency.getId() == R.id.root;
    }


    @Override
    public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
        Log.e("tag","onDependentViewChanged");
        if (!isinit){
            child.setTranslationY(0);
        }


        return true;
    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {

        return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }


    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {

        NestedScrollView nestedScrollView = (NestedScrollView) target;
        boolean isreachbottom = reachBottom(nestedScrollView);
        boolean isreachtop = reachTop(nestedScrollView);
        Log.e("tag", "dy--->" + dy + "---isreachtop--" + isreachtop + "---target.getY()--》" + target.getY());
        Log.e("tag","isreachbottom---->"+isreachbottom+"----type---"+type);
           if (type == ViewCompat.TYPE_TOUCH){

               if (target.getY() >= 0) {
                   Log.e("tag","---target.getY()----"+target.getY());
                    consumed[1] = dy;
                   target.setY(target.getY() -dy);
                   float top =  target.getY();
                   Log.e("tag","top----111------>"+dy+"-----getY--"+target.getY());


                   float height = child.getMeasuredHeight();
                   float scale = (height +top) / height;
                   scale = Math.max(1f, scale);

                   child.setPivotY(0);
                   child.setScaleX(scale);
                   child.setScaleY(scale);
                   child.setY(0);

                   Log.e("tag", "sacale=----->" + scale);

               } else {

//                 向上滑动的时候需要判断target是否滑动到底部，如果没有，先滑动到底部
                   consumed[1] = dy;
                   target.setY(target.getY() -dy);
                   if (dy < 0 && child.getY() -dy >0 ){

                       child.setY(0);
                   }else {
                       child.setY(child.getY()-dy);
                   }
                   child.setScaleX(1);
                   child.setScaleY(1);

               }
           }else {
//               fling状态
              consumed[1] = 0;


           }
    }


    @Override
    public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int type) {
        super.onStopNestedScroll(coordinatorLayout, child, target, type);
        Log.e("tag", "onStopNestedScroll");
        if(type == ViewCompat.TYPE_TOUCH && target.getY() !=0){
            reset(target,child,200);
        }
    }


    private void init(View child) {
        mHeaderHeight = child.getMeasuredHeight();
        mHeaderWidth = child.getMeasuredWidth();
    }


    //    判断是否滑动到顶部
    public boolean reachBottom(NestedScrollView target) {

//        获取嵌套滑动的子view
        View childView = target.getChildAt(0);
        return childView != null && childView.getMeasuredHeight() == target.getScrollY() + target.getHeight();
    }

//     判断是否滑动到顶部

    public boolean reachTop(NestedScrollView target) {



        return null != target && target.getScrollY() == 0;
    }


    public void reset(View target, View child, long duration) {
        if (target == null) {
            return;
        }
        target.clearAnimation();
        child.clearAnimation();
        ObjectAnimator.ofFloat(target, "Y", 0F).setDuration(duration).start();
        ObjectAnimator.ofFloat(child,"Y",0F).setDuration(duration).start();
        ObjectAnimator.ofFloat(child, "ScaleX", 1F).setDuration(duration).start();
        ObjectAnimator.ofFloat(child, "ScaleY", 1F).setDuration(duration).start();

    }
}
